package com.demoo.zjsu.service.smartwebsite.zjgsu;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.service.smartwebsite.*;
import com.demoo.zjsu.string.smartwebsite.WebsiteStrings;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/17.
 * 处理官网首页的一些展示内容（需要加入一些内容的省略）
 */
@Service
public class IndexPageService extends IFactory{


    @Override
    public Page getPage() {
        return null;
    }

    @Autowired
    SmartWebDAO dao;

    @Override
    public void resolver() {
        Document document = WebsiteUtil.getDocByURL("http://www.hzic.edu.cn/");
        Elements elements = document.select(".newsbody").select("li");
        List<PageTwo> pageTwos = new ArrayList<PageTwo>();
        List<PageTwoContent> contents = new ArrayList<PageTwoContent>();
        int size = elements.size();
        Elements elements2 = document.select(".newsinfo_L").select("li");

        dealPhotoAndFile(elements2);
        for (int i = 0; i < size; i++) {
            Elements element = elements.eq(i);
            String title = element.text().trim();
            int index = title.lastIndexOf("（");
            String date = title.substring(index + 1, title.length() - 1);
            String href = element.select("a").attr("href").replace(".html","");
            String abshref = element.select("a").attr("abs:href");
            title = title.substring(0, index);
            PageTwo pageTwo = new PageTwo(title, date, href, 0);
            String category = null;
            if (i<=12) {
                category = "商大新闻";
            } else if (i <= 24) {
                category = "公告";
            } else if (i <= 36) {
                category = "学术";
            } else if (i <= 48) {
                category = "交流";
            } else if (i <= 60) {
                category = "文体";
            }
            pageTwo.setCategory(category);
            pageTwos.add(pageTwo);
            getOtherMain(contents, href, abshref);
        }

        for (Element element : elements2) {
            Elements img = element.select("img");
            String key = img.attr("src");
            String href = element.select("a").attr("href").replace(".html","");
            String abshref = element.select("a").attr("abs:href");
            PageTwo pageTwo = new PageTwo(key,"",href,1);
            pageTwo.setCategory("商大新闻");
            String date = getOtherMain(contents, href, abshref);
            pageTwo.setDate(date);
            pageTwos.add(pageTwo);
        }
        dao.insertListPageTwo(pageTwos);
        dao.insertListPageTwoContent(contents);
    }

    public String getOtherMain(List<PageTwoContent> contents, String href, String abshref) {
        Document document;
        document = WebsiteUtil.getDocByURL(abshref);
        Elements elements1 = getMain(document);
        filterMain(elements1);
        filterElse(elements1);
        dealPhotoAndFile(elements1);
        String throat = elements1.select("tr").eq(1).text().replaceAll("【.*】", "");
        String content = elements1.select("tr").eq(2).toString();
        PageTwoContent pageTwoContent = new PageTwoContent(content, throat);
        pageTwoContent.setKey(href);
        contents.add(pageTwoContent);
        String title = elements1.select("tr").eq(0).text();
        return title;
    }

    @Override
    public Elements getMain(Document document) {
        return document.select("table[width=700]");
    }

    @Override
    public void filterElse(Elements elements) {
        elements.select("tr").eq(3).remove();
        Elements element = elements.select("a");
        for (Element element1 : element) {
            String a = element1.attr("href");
            a = a.replace("http://www.zjgsu.edu.cn", "");
            element1.attr("href", a);
        }
        Elements img = elements.select("img");
        for (Element element1 : img) {
            String a = element1.attr("src");
            a = a.replace("http://www.zjgsu.edu.cn", "");
            element1.attr("src", a);
        }

    }

    @Override
    public void addPhoto(Elements elements) {

    }

    @Override
    public void addFile(Elements elements) {

    }
}
